package com.mtt.erp.logistics.controller;

import com.alibaba.fastjson.JSONObject;
import com.mtt.erp.common.annotation.Log;
import com.mtt.erp.common.api.R;
import com.mtt.erp.common.api.ResultCode;
import com.mtt.erp.common.kdniao.KdniaoUtils;
import com.mtt.erp.logistics.dto.FaceOrderPrintDto;
import com.mtt.erp.logistics.dto.OrderFaceTempSaveDto;
import com.mtt.erp.logistics.dto.TimelySearchDto;
import com.mtt.erp.logistics.service.IOrderFaceTempleteService;
import com.mtt.erp.order.dto.MbaFileTrackingDto;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@Slf4j
@Validated
@RestController
@RequestMapping("/logistics")
@Api(value = "面单相关",tags = "M004-物流-电子面单")
public class LogisticsController{

    @Autowired
    IOrderFaceTempleteService iOrdeFaceTempleteService;

    /**
     * 电子面单
     * @param faceOrderPrintDto
     * @return
     */
    @Log("打印电子面单")
    @PostMapping("eOrder")
    @ApiOperation(value = "打印电子面单",notes = "打印电子面单,传入面单信息")
    public R md(@RequestBody FaceOrderPrintDto faceOrderPrintDto){
        try {
            //返回打印模板
            faceOrderPrintDto.setIsReturnPrintTemplate(1);
            String requestData = JSONObject.toJSONString(faceOrderPrintDto);
            String result = KdniaoUtils.orderOnlineByJson(requestData);

            //根据公司业务处理返回的信息......
            JSONObject resultJson = JSONObject.parseObject(result);

            //面单html
            String printTemplate=resultJson.getString("PrintTemplate");
            if(resultJson.getBoolean("Success")){
                //物流面单存储操作
                JSONObject orderJson = resultJson.getJSONObject("Order");
                //订单编号
                String orderCode=orderJson.getString("OrderCode");
                //物流公司编码
                String shipperCode=orderJson.getString("ShipperCode");
                //物流单号
                String logisticCode=orderJson.getString("LogisticCode");
                //保存面单到数据库
                Boolean saveOrderFaceTemplete = iOrdeFaceTempleteService.saveOrderFaceTemplete(OrderFaceTempSaveDto.builder()
                        .logisticCode(logisticCode)
                        .orderCode(orderCode)
                        .shipperCode(shipperCode)
                        .printTemplate(printTemplate).build());
                if(saveOrderFaceTemplete){
                    return R.data(result);
                }else{
                    return R.fail(ResultCode.FAILURE);
                }
            }else{
                return R.fail(ResultCode.FAILURE);
            }
        }catch (Exception e){
            return R.fail(ResultCode.FAILURE);
        }
    }
    /**
     * 订单追踪
     *
     * @param mbaFileTrackingDto
     * @return
     */
    @Log("订单追踪")
    @PutMapping("mbaFileTrackingDto")
    @ApiOperation(value = "订单跟踪",notes = "订单跟踪,暂未开发")
    public R mbaFileTracking(MbaFileTrackingDto mbaFileTrackingDto) {
        return null;
    }

    /**
     * 及时查询
     * @param timelySearchDto
     * @return
     */
    @Log("及时查询")
    @GetMapping("timelySearch")
    @ApiOperation(value = "及时查询",notes = "及时查询订单(暂未使用)")
    public R timelySearch(@RequestBody TimelySearchDto timelySearchDto){
        try {
            String result = KdniaoUtils.orderHandle(timelySearchDto.getShipperCode(), timelySearchDto.getLogisticCode());
            if(null != result && !"".equals(result)){
                return R.data(result);
            }else{
                return R.fail(ResultCode.NO_CONTENT);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(ResultCode.NO_CONTENT);
        }
    }
}
